(function (){

$(".back").click(function () {
    $("body,html").animate({ scrollTop: 0 }, 500)
    console.log("aaa")
})

$.ajax({
    type: "GET",
    url: "http://chst.vip:1234/api/getcategorytitle",
    data: {}
}).then(res => {
    let html = "";
    data = res.result;
    data.forEach(function (item) {
        html += `
        <li>
            <p>${item.title}</p>
            <div></div>
        </li>
        <div class="container"></div>
        `
    })
    html+=""
    $(".list").html(html);
    $(".list").children("li").click(function(){
         if($(this).next().css("max-height")!="0px"){
            $(this).next().css("max-height","0px");
         }else{
            $(this).next().css("max-height","6rem");
            $(this).next().siblings("div").css("max-height","0rem")
         }
    })
    
})
        let length=[];
function fn(x) { 
    $.ajax({
        type: "GET",
        url: "http://chst.vip:1234/api/getcategory",
        data: {titleid:x}
    }).then(res=>{
        let arr=res.result;
        let html="";
        length[x]=arr.length
        arr.forEach(function(item){
            html+=`
                <div>${item.category}</div>
            `
        })
        $(".container:eq("+x+")").html(html);


        $(".container:eq("+x+")").children("div").click(function(){
            let count=0;
            for(var i=0;i<x;i++){
                count+=length[i]
            }

            var id=count+$(this).index()
            var text=$(this).html()
            window.location.href="http://127.0.0.1:5500/%E9%A1%B9%E7%9B%AE/pages/shoppingList.html?categoryid="+id+"&category="+text;
        })
        
    })
 }
 for(var i=0;i<8;i++){
     fn(i);
 }

 


})()